import { getClassAndStyleList } from '../../../api/index'

Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    active: {
      type: String,
      value: ''
    }
  },
  data: {
    styleList: [],
    defaultIndex: 0
  },
  methods: {
    close () {
      this.triggerEvent('close')
    },
    confirm () {
      this.triggerEvent('change', this.data.styleList)
    },
    select ({ currentTarget: { dataset: { item, index } } }) {
      let key = `styleList[${index}].isSelect`
      this.setData({ [key]: !item.isSelect })
    },
    async getStyleList () {
      try {
        const { ResultSet } = await getClassAndStyleList()
        let index = ResultSet.userData.stylesList.findIndex(item => item.value === parseInt(this.data.active))
        if (index > -1) this.setData({ defaultIndex: index })
        ResultSet.userData.stylesList.forEach(item => {
          item.isSelect = false
        })
        this.setData({ styleList: ResultSet.userData.stylesList })
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    beforeOpen () {
      this.getStyleList()
    }
  }
})